<template>
  <div>
    <div v-loading="loading"
         v-show="showPannel===0"
         class="edu-page-warp">
      <ConditionCard shadow="never"
                     body-style="padding: 24px 24px 6px 24px;"
                     @search="refreshPage"
                     @reset="reset('queryForm')">
        <el-form ref="queryForm"
                 :model="queryForm"
                 label-position="right"
                 label-width="95px">
          <el-row :gutter="20"
                  style="height: 50px;">
              <el-col :span="8">
            <el-form-item label="年级">
              <GradeSelect v-model="queryForm.grade"></GradeSelect>
            </el-form-item>
          </el-col>
            <el-col :span="8">
              <el-form-item label="学院">
                <DictSelect
                    @change="changeFaculty()"
                    v-model="queryForm.faculty"
                    :data="options.faculty"
                    :clearable="true"
                    :filterable="true"
                ></DictSelect>
              </el-form-item>
            </el-col>
              <el-col :span="8">
                  <el-form-item label="专业">
                      <DictSelect
                          v-model="queryForm.profession"
                          :data="options.profession"
                          :clearable="true"
                          :filterable="true"
                      ></DictSelect>
                  </el-form-item>
              </el-col>
          </el-row>
          <el-row :gutter="20"
              style="height: 50px;">
              <el-col :span="8">
                  <el-form-item label="辅修行政学院">
                      <DictSelect
                          @change="majorNameChange(2)"
                          v-model="queryForm.minorFaculty"
                          :data="options.minorFaculty"
                          :clearable="true"
                          :filterable="true"
                      ></DictSelect>
                  </el-form-item>
              </el-col>
              <el-col :span="8">
                  <el-form-item label="辅修专业">
                      <DictSelect
                          v-model="queryForm.minorProfession"
                          :data="options.minorProfession"
                          :clearable="true"
                          :filterable="true"
                      ></DictSelect>
                  </el-form-item>
              </el-col>
              <el-col :span="8">
                    <el-form-item label="进阶层次:" prop="advancedLevel" class="no-bottom">
                        <el-select
                            clearable
                            v-model="queryForm.advancedLevel"
                            style="width:100%"
                            @change="queryMinorMajorCondition"
                            placeholder="请选择"
                        >
                            <el-option
                                v-for="item in options.advancedLevel"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value"
                            ></el-option>
                        </el-select>
                    </el-form-item>
              </el-col>
          </el-row>
        </el-form>
      </ConditionCard>
      <el-card v-loading="loading"
               shadow="never"
               body-style="padding: 24px;">
        <div class="first_title">学生成绩打印</div>
        <div class="float-left">
          <el-input v-model="queryForm.studentIdOrName"
                    :placeholder="$t('score.failScoreManage.pleaseInt')"
                    maxlength="50"
                    prefix-icon="el-icon-search"
                    @change="handSearchChang" />
        </div>
        <div style="float:right;">
            <el-dropdown placement='bottom-start' trigger="click" @command="handlePrint">
                <el-button type="primary">批量打印<i class="el-icon-arrow-down el-icon--right"></i></el-button>
                <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item command="cnPrint">中文成绩单</el-dropdown-item><!-- 中文成绩单 -->
                    <el-dropdown-item command="enPrint">英文成绩单</el-dropdown-item><!-- 英文成绩单 -->
                </el-dropdown-menu>
            </el-dropdown>
        </div>
        <div style="float:right; margin:0px 10px;">
            <el-dropdown placement='bottom-start' trigger="click" @command="handleExport">
                <el-button type="primary">批量导出<i class="el-icon-arrow-down el-icon--right"></i></el-button>
                <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item command="cnExport">中文批量导出</el-dropdown-item><!-- 中文批量导出 -->
                    <el-dropdown-item command="enExport">英文批量导出</el-dropdown-item><!-- 英文批量导出 -->
                </el-dropdown-menu>
            </el-dropdown>
        </div>
        <EduTable :data="tableList"
                  :order="false"
                  :total="pageParam.total"
                  class="scoreTableMarginTop"
                  @page-change="pageChange"
                  @selection-change="handleSelectionChange"
        >
          <el-table-column :label="$t('label.stuCode')"
                           prop="studentId"
                           min-width='100'
                           show-overflow-tooltip="true"
                           align="center" />
          <el-table-column :label="$t('label.stuName')"
                           prop="studentName"
                           show-overflow-tooltip="true"
                           min-width='100'
                           align="center" />
          <el-table-column :label="$t('label.grade')"
                           prop="grade"
                           min-width='80'
                           show-overflow-tooltip="true"
                           align="center" />
          <el-table-column :label="$t('label.college')"
                           prop="facultyI18n"
                             min-width='150'
                           show-overflow-tooltip="true"
                           align="center" />
          <el-table-column label="专业"
                           min-width='150'
                           prop="professionI18n"
                           show-overflow-tooltip="true"
                           align="center" />
          <el-table-column label="辅修行政学院"
                             min-width='150'
                           prop="minorFacultyI18n"
                           show-overflow-tooltip="true"
                           align="center" />
          <el-table-column label="辅修专业"
                             min-width='100'
                           prop="minorProfessionI18n"
                           show-overflow-tooltip="true"
                           align="center">
          </el-table-column>
          <el-table-column label="进阶层次"
                             min-width='100'
                           prop="advancedLevelName"
                           show-overflow-tooltip="true"
                           align="center">
          </el-table-column>
          <el-table-column label="辅修微专业入学年月"
                             min-width='150'
                           prop="entranceTerm"
                           show-overflow-tooltip="true"
                           align="center">
              <template slot-scope="scope">
                {{scope.row.entranceTerm}}
              </template>
          </el-table-column>
          <el-table-column :label="$t('score.studebtScoreQueryForLeave.chineseBlanket')" min-width="100"  fixed="right"
                           align="center">
            <template slot-scope="scope">
              <el-button type="text" @click="showChineseBlanket(scope.row)">{{ $t('score.preview') }}</el-button>
            </template>
          </el-table-column>
          <el-table-column :label="$t('score.studebtScoreQueryForLeave.englishBlanket')" min-width="100"  fixed="right"
                           align="center">
            <template slot-scope="scope">
              <el-button type="text" @click="showEnglishBlanket(scope.row)">{{ $t('score.preview') }}</el-button>
            </template>
          </el-table-column>
        </EduTable>
      </el-card>
    </div>
    <div v-if="showPannel === 1">
      <minorChineseBlanket  :chineseScoremessage="chineseScoremessage" @back="goBack" />
    </div>
    <div v-else-if="showPannel === 2">
      <minorEnglishBlanket :englishScoremessage="englishScoremessage" @back='goBack'/>
    </div>
    <edu-export-dialog title="提示" ref="downloadRef" :data="exportResult" @close="closeFresh"></edu-export-dialog>
  </div>
</template>
<script>
import UTILS from 'common/src/utils/utils';
import filters from 'common/src/filters';
import { queryDic } from 'common/src/api/dictionary';
import { queryProfessionByCollege } from "common/src/api/minor/minorCultureScheme";
import scoreManagementApi from 'common/src/api/scoreManagement/scoreManagement';
import minorChineseBlanket from './minorChineseBlanket.vue';
import minorEnglishBlanket from './minorEnglishBlanket.vue';
import { queryAdvancedLevel } from "common/src/api/minor/collegemajor";
export default {
    name: 'minor-score-print',
    components: {
      minorChineseBlanket,
      minorEnglishBlanket
    },
    filters: {
        formatDate(date) {
            return filters.formatDate(new Date(date), 'yyyy-MM-dd');
        }
    },
    data () {
        return {
            importTime: null, //轮询导入
            importKey: '', // 导入获取的key
            exportResult: null,
            multipleSelection: [],
            tableList: [],
            pageParam: {
                pageNum_: 1,
                pageSize_: 20,
                total: 0
            },
            queryForm: {
                grade:'',
                faculty: '',
                profession:'',
                minorFaculty: '',
                minorProfession: '',
                studentIdOrName: '',
                advancedLevel: '' // 进阶层次
            },
            showPannel: 0,
            options: {
                faculty: [],
                profession: [],
                minorProfession: [],
                minorFaculty: [],
                advancedLevel: []
            }
        };
    },
    mounted () {
        this.refreshPage();
        this.getOptions();
        this.initAdvancedLevel("0");
    },
    methods: {
        // 勾选数据
        handleSelectionChange(val) {
            this.multipleSelection = val
        },
        // 批量打印
        handlePrint(val) {
            if (this.multipleSelection.length == 0) {
                this.$message(this.$t('scoreMangementBase.pleaseChoose'))
                return
            }
            if (this.multipleSelection.length > 0 && val === 'cnPrint') {
                let arr = []
                let paramsData = {}
                this.multipleSelection.map(value => {
                    arr.push({
                        studentId: value.studentId?value.studentId: '',
                        minorFaculty: value.minorFaculty?value.minorFaculty: '',
                        minorProfession: value.minorProfession?value.minorProfession: '',
                        advancedLevel: value.advancedLevelCode?value.advancedLevelCode: ''
                    })
                })
                paramsData = {
                    language: 'cn',
                    type: '1'
                }
                scoreManagementApi.exportBatch(arr,paramsData).then(res => {
                    this.importKey = res.data.key
                })
            } else if (this.multipleSelection.length > 0 && val === 'enPrint') {
                let arr = []
                let paramsData = {}
                this.multipleSelection.map(value => {
                    arr.push({
                        studentId: value.studentId?value.studentId: '',
                        minorFaculty: value.minorFaculty?value.minorFaculty: '',
                        minorProfession: value.minorProfession?value.minorProfession: '',
                        advancedLevel: value.advancedLevelCode?value.advancedLevelCode: ''
                    })
                })
                paramsData = {
                    language: 'en',
                    type: '1'
                }
                scoreManagementApi.exportBatch(arr,paramsData).then(res => {
                    this.importKey = res.data.key
                })
            }
            if (this.importKey!=null || this.importKey!='') {
                this.$refs.downloadRef.open();
                this.importTime = setInterval(() => {
                    scoreManagementApi.exportBatchForKey(this.importKey).then(resT => {
                        this.exportResult = resT.data
                        clearInterval(this.importTime);
                        if (resT.code === 200 && resT.data.status && resT.data.success) {
                            let path = '/api/scoremanagementservice/minorStudentScoreManager/exportZip?path=' + resT.data.path;
                            window.open(path)
                            this.$refs.downloadRef.close();
                        } else {
                            this.$refs.downloadRef.close();
                            return this.$message('该学生暂无成绩')
                        }
                    })
                }, 2000);
            }
        },
        // 批量导出
        handleExport(val) {
            if (this.multipleSelection.length == 0) {
                this.$message(this.$t('scoreMangementBase.pleaseChoose'))
                return
            }
            if (this.multipleSelection.length > 0 && val === 'cnExport') {
                let arr = []
                let paramsData = {}
                this.multipleSelection.map(value => {
                    arr.push({
                        studentId: value.studentId?value.studentId: '',
                        minorFaculty: value.minorFaculty?value.minorFaculty: '',
                        minorProfession: value.minorProfession?value.minorProfession: '',
                        advancedLevel: value.advancedLevelCode?value.advancedLevelCode: ''
                    })
                })
                paramsData = {
                    language: 'cn',
                    type: '0'
                }
                scoreManagementApi.exportBatch(arr,paramsData).then(res => {
                    this.importKey = res.data.key
                })
            } else if (this.multipleSelection.length > 0 && val === 'enExport') {
                let arr = []
                let paramsData = {}
                this.multipleSelection.map(value => {
                    arr.push({
                        studentId: value.studentId?value.studentId: '',
                        minorFaculty: value.minorFaculty?value.minorFaculty: '',
                        minorProfession: value.minorProfession?value.minorProfession: '',
                        advancedLevel: value.advancedLevelCode?value.advancedLevelCode: ''
                    })
                })
                paramsData = {
                    language: 'en',
                    type: '0'
                }
                scoreManagementApi.exportBatch(arr,paramsData).then(res => {
                    this.importKey = res.data.key
                })
            }
            if (this.importKey!=null || this.importKey!='') {
                this.$refs.downloadRef.open();
                this.importTime = setInterval(() => {
                    scoreManagementApi.exportBatchForKey(this.importKey).then(resT => {
                        this.exportResult = resT.data
                        clearInterval(this.importTime);
                        if (resT.code === 200 && resT.data.status && resT.data.success) {
                            let path = '/api/scoremanagementservice/minorStudentScoreManager/exportZip?path=' + resT.data.path;
                            window.open(path)
                            this.$refs.downloadRef.close();
                        } else {
                            this.$refs.downloadRef.close();
                            return this.$message('该学生暂无成绩')
                        }
                    })
                }, 2000);
            }
        },
        /**
         * 根据学院获取专业下拉列表
         */
        getOptions() {
            let data = {
                lang: this.$store.getters.language || "cn",
                type: "allChild",
                keys: ["X_YX", "G_ZY"]
            };
            queryDic(data).then(res => {
                this.options.college = [];
                UTILS.fillSelect(
                    {
                        faculty: "X_YX",
                        profession: "G_ZY",
                        minorFaculty: "X_YX"
                    },
                    res.data,
                    this.options
                );
            });
        },
        // 学院改变时获取专业 
        changeFaculty() {
            this.options.profession = [];
            this.queryForm.profession = "";
            let facultys = this.queryForm.faculty;
            const data = {
                lang: this.$store.getters.language || "cn",
                type: "allChild",
                keys: ["G_ZY"],
                filter: { prefix: "2", specilaCode: "XY2ZY", facultys: facultys, grade: this.queryForm.grade, degreeType: [] }
            };
            queryDic(data).then(res => {
                if (res.code == 200){
                    UTILS.fillSelect({profession: "G_ZY"}, res.data, this.options );
                }
            });
        },
        majorNameChange(type) {
            let params = {
                collegeCode: type === 1 ? this.queryForm.faculty : this.queryForm.minorFaculty
            };
            if (!params.collegeCode) {
                if (type === 1) {
                    this.queryForm.profession = "";
                    this.options.profession = [];
                } else {
                    this.queryForm.minorProfession = "";
                    this.options.minorProfession = [];
                }
                return;
            }
            queryProfessionByCollege(params).then(res => {
                if (res.code == 200) {
                    if (type === 1) {
                        this.queryForm.profession = "";
                        this.options.profession = res.data.map(el => {
                            return el;
                        });
                    } else {
                        this.queryForm.minorProfession = "";
                        this.options.minorProfession = res.data.map(el => {
                            return el;
                        });
                    }
                }
            });
        },
        // 进阶层次
        initAdvancedLevel(isOnCampus) {
            let params = {
                isOnCampus: isOnCampus ? isOnCampus : "0"
            };
            this.options["advancedLevel"] = [];
            queryAdvancedLevel(params).then(res => {
                if (res.code == 200) {
                    for (let key = 0; key < res.data.length; key++) {
                        this.options["advancedLevel"].push({
                            value: res.data[key].advancedLevelCode,
                            label: res.data[key].advancedLevelName
                        });
                    }
                }
            });
        },
        // 刷新页面信息
        refreshPage () {
            let param = {};
            param.pageNum_ = this.pageParam.pageNum_;
            param.pageSize_ = this.pageParam.pageSize_;
            param.condition = this.queryForm;
            scoreManagementApi.getScoreListPage(param).then(({code, data})=>{
                if (code != 200) return;
                this.tableList = data.list.map( val => {
                        // this.$nextTick(()=>{
                        //     val.enrolDate = val.enrolDate.substring(0, 10)
                        // })
                        return val;
                })
                this.pageParam.total = data.total
            })
        },
        // 重置页面筛选条件
        reset () {
            for (let key in this.queryForm) {
                if (key === 'calendarId') {
                    continue;
                }
                this.queryForm[key] = '';
            }
            this.refreshPage();
        },
        // 根据输入内容连同筛选条件一起刷新页面信息
        handSearchChang () {
            this.refreshPage();
        },
        // 中文成绩单
        showChineseBlanket (row) {
            let param = {
                studentId: row.studentId,
                minorFaculty: row.minorFaculty,
                minorProfession: row.minorProfession,
                language: 'cn',
                advancedLevel: row.advancedLevelCode
            };
            scoreManagementApi.getScorePreview(param).then(res => {
                if(res.code === 200) {
                    this.chineseScoremessage = row
                    this.showPannel = 1;
                }
            });
        },
        showEnglishBlanket (row) {
            let param = {
                studentId: row.studentId,
                minorFaculty: row.minorFaculty,
                minorProfession: row.minorProfession,
                language: 'en',
                advancedLevel: row.advancedLevelCode
            };
            scoreManagementApi.getScorePreview(param).then(res => {
                if(res.code === 200) {
                    this.englishScoremessage = row
                    this.showPannel = 2;
                }
            });
        },
        // 分页查询
        pageChange (val) {
            this.pageParam.pageNum_ = val.pageNum;
            this.pageParam.pageSize_ = val.pageSize;
            this.refreshPage();
        },
        // 返回主页面
        goBack(){
            this.showPannel = 0
            // this.refreshPage()
        }
    }
};
</script>
<style lang="scss" scoped>
.first_title {
    font-size: 18px;
    font-family: "微软雅黑";
    color: #2b3b4e;
    padding: 0px 0px 16px;
}
.scoreTableMarginTop {
    clear: both;
    position: relative;
    top: 20px;
}
</style>